'use strict';
const controller = require('egg').Controller;

class HomeController extends controller {
  async getArticleList() {
    // 博客列表
    const { ctx } = this;
    const sql = 'SELECT article.id as id,' +
      'article.title as title,' +
      'article.introduce as introduce,' +
      'article.addTime as addTime,' +
      'article.view_count as view_count ,' +
      '.type.typeName as typeName ' +
      'FROM article LEFT JOIN type ON article.type_id = type.Id';
    const result = await this.app.mysql.query(sql);
    ctx.body = {
      data: result,
    };
  }

  async getArticleById() {
    // 博客详情
    const { ctx } = this;
    const sql = 'SELECT article.id as id,' +
      'article.title as title,' +
      'article.introduce as introduce,' +
      'article.article_content as article_content,' +
      'article.addTime as addTime,' +
      'article.view_count as view_count ,' +
      '.type.typeName as typeName ' +
      `FROM article LEFT JOIN type ON article.type_id = type.Id WHERE article.id=${ctx.params.id}`;
    const result = await this.app.mysql.query(sql);
    ctx.body = {
      data: result,
    };
  }

  async getListById() {
    // 根据类别ID获得文章列表
    const { ctx } = this;
    const sql = 'SELECT article.id as id,' +
      'article.title as title,' +
      'article.introduce as introduce,' +
      'article.addTime as addTime,' +
      'article.view_count as view_count ,' +
      '.type.typeName as typeName ' +
      `FROM article LEFT JOIN type ON article.type_id = type.Id WHERE article.type_id=${ctx.params.id}`;
    const result = await this.app.mysql.query(sql);
    ctx.body = {
      data: result,
    };
  }
}

module.exports = HomeController;
